package com.pay.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 
 * @Description: 这里用一句话描述这个类的作用
 * @see: MyBatisOraConfig 此处填写需要参考的类
 * @version 2017年4月13日 下午5:36:36 
 * @author jian.cao
 */
@Configuration
@AutoConfigureAfter(MariaDBConfig.class)
@MapperScan(sqlSessionFactoryRef = "bossSqlSessionFactory", basePackages = "com.pay.mapper.trans")
@EnableTransactionManagement(order = 0)
public class TransMyBatisConfig {
	@Resource(name="transDataSource")
	private DataSource transDataSource;

	@Bean(name="transSqlSessionFactory")
	public SqlSessionFactory transSqlSessionFactory() throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(transDataSource);
		bean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
		return bean.getObject();
	}

	@Bean(name="transDataSourceTransactionManager")
	public DataSourceTransactionManager transDataSourceTransactionManager() {
		return new DataSourceTransactionManager(transDataSource);
	}

	@Bean(name="transJdbcTemplate")
	public JdbcTemplate transJdbcTemplate() {
		return new JdbcTemplate(transDataSource);
	}


}
